﻿using System.Web.Mvc;

namespace CodeContrib.Web.Mvc.Html
{
	public static class ScriptExtensions
	{
		private static readonly string _jQueryLocalizationUrl;
		private static readonly string _jQueryMinifiedUrl;
		private static readonly string _jQueryUIMinifiedUrl;
		private static readonly string _jQueryUIUrl;
		private static readonly string _jQueryUrl;

		static ScriptExtensions()
		{
			const string jqueryVersion = "1.8.0";
			const string jqueryUIVersion = "1.8.23";

			_jQueryLocalizationUrl = string.Format("http://ajax.googleapis.com/ajax/libs/jqueryui/{0}/i18n/jquery-ui-i18n.min.js", jqueryUIVersion);
			_jQueryMinifiedUrl = string.Format("http://ajax.googleapis.com/ajax/libs/jquery/{0}/jquery.min.js", jqueryVersion);
			_jQueryUIUrl = string.Format("http://ajax.googleapis.com/ajax/libs/jqueryui/{0}/jquery-ui.js", jqueryUIVersion);
			_jQueryUIMinifiedUrl = string.Format("http://ajax.googleapis.com/ajax/libs/jqueryui/{0}/jquery-ui.min.js", jqueryUIVersion);
			_jQueryUrl = string.Format("http://ajax.googleapis.com/ajax/libs/jquery/{0}/jquery.js", jqueryVersion);
		}

		// jQuery Unobstrusive?
		// jQuery Validate?
		// jQuery Themes?

		public static MvcHtmlString JQuery(this HtmlHelper htmlHelper, string localUrl)
		{
			return ScriptCdn(htmlHelper, _jQueryUrl, localUrl, "!window.jQuery");
		}

		public static MvcHtmlString JQueryLocalization(this HtmlHelper htmlHelper)
		{
			return Script(htmlHelper, _jQueryLocalizationUrl);
		}

		public static MvcHtmlString JQueryMinified(this HtmlHelper htmlHelper, string localUrl)
		{
			return ScriptCdn(htmlHelper, _jQueryMinifiedUrl, localUrl, "!window.jQuery");
		}

		public static MvcHtmlString JQueryUI(this HtmlHelper htmlHelper, string localUrl)
		{
			return ScriptCdn(htmlHelper, _jQueryUIUrl, localUrl, "!window.jQuery.ui");
		}

		public static MvcHtmlString JQueryUIMinified(this HtmlHelper htmlHelper, string localUrl)
		{
			return ScriptCdn(htmlHelper, _jQueryUIMinifiedUrl, localUrl, "!window.jQuery.ui");
		}

		public static MvcHtmlString Script(this HtmlHelper htmlHelper, string url)
		{
			Guard.NotNullOrEmpty(url, "url");

			var script = new TagBuilder("script");
			script.Attributes["src"] = url.VerifyUrlProtocol();
			script.Attributes["type"] = "text/javascript";

			return MvcHtmlString.Create(script.ToString());
		}

		public static MvcHtmlString ScriptCdn(this HtmlHelper htmlHelper, string cdnUrl, string localUrl, string javaScriptCondition)
		{
			var cdnScript = Script(htmlHelper, cdnUrl);

			if (string.IsNullOrEmpty(localUrl))
			{
				return cdnScript;
			}

			var localScript = new TagBuilder("script")
				                  {
					                  InnerHtml = string.Format("{0} && document.write(unescape('%3Cscript src=\"{1}\" type=\"text/javascript\"%3E%3C/script%3E'))", javaScriptCondition, localUrl)
				                  };
			localScript.Attributes["type"] = "text/javascript";

			return MvcHtmlString.Create(string.Concat(cdnScript, localScript));
		}
	}
}